<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-master-table tb-absolute-fill">
  <div class="tb-form-panel no-border no-padding padding-top">
    <div class="tb-form-hint tb-primary-fill tb-flex center">{{ 'gateway.hints.modbus-master' | translate }}</div>
  </div>
  <div fxFlex fxLayout="column" class="tb-master-table-content">
    <mat-toolbar class="mat-mdc-table-toolbar" [fxShow]="!textSearchMode">
      <div class="mat-toolbar-tools" *ngIf="(dataSource.isEmpty() | async) === false">
        <div fxLayout="row" fxLayoutAlign="start center" fxLayout.xs="column" fxLayoutAlign.xs="center start" class="title-container">
          <span class="tb-master-table-title">{{ 'gateway.servers-slaves' | translate}}</span>
        </div>
        <span fxFlex></span>
        <button mat-icon-button
                (click)="manageSlave($event)"
                matTooltip="{{ 'action.add' | translate }}"
                matTooltipPosition="above">
          <mat-icon>add</mat-icon>
        </button>
        <button mat-icon-button
                (click)="enterFilterMode()"
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" [fxShow]="textSearchMode">
      <div class="mat-toolbar-tools">
        <button mat-icon-button
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
        <mat-form-field fxFlex>
          <mat-label>&nbsp;</mat-label>
          <input #searchInput matInput
                 [formControl]="textSearch"
                 placeholder="{{ 'common.enter-search' | translate }}"/>
        </mat-form-field>
        <button mat-icon-button (click)="exitFilterMode()"
                matTooltip="{{ 'action.close' | translate }}"
                matTooltipPosition="above">
          <mat-icon>close</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <div class="table-container">
      <table mat-table [dataSource]="dataSource">
        <ng-container [matColumnDef]="'deviceName'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            <div tbTruncateWithTooltip>{{ 'gateway.device-name' | translate }}</div>
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            <div tbTruncateWithTooltip>{{ slave['deviceName'] }}</div>
          </mat-cell>
        </ng-container>
        <ng-container [matColumnDef]="'info'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            {{ 'gateway.info' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            <div tbTruncateWithTooltip>{{ slave['host'] ?? slave['port'] }}</div>
          </mat-cell>
        </ng-container>
        <ng-container [matColumnDef]="'unitId'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            {{ 'gateway.unit-id' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            <div tbTruncateWithTooltip>{{ slave['unitId'] }}</div>
          </mat-cell>
        </ng-container>
        <ng-container [matColumnDef]="'type'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            <div>{{ 'gateway.type' | translate }}</div>
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            {{ ModbusProtocolLabelsMap.get(slave['type']) }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="actions" stickyEnd>
          <mat-header-cell *matHeaderCellDef
                           [ngStyle.gt-md]="{ minWidth: '96px', maxWidth: '96px', width: '96px', textAlign: 'center'}">
          </mat-header-cell>
          <mat-cell *matCellDef="let slave; let i = index"
                    [ngStyle.gt-md]="{ minWidth: '96px', maxWidth: '96px', width: '96px'}">
            <ng-template #rowActions>
              <button mat-icon-button
                      matTooltip="{{ 'action.edit' | translate }}"
                      matTooltipPosition="above"
                      (click)="manageSlave($event, i)">
                <tb-icon>edit</tb-icon>
              </button>
              <button mat-icon-button
                      matTooltip="{{ 'action.delete' | translate }}"
                      matTooltipPosition="above"
                      (click)="deleteSlave($event, i)">
                <tb-icon>delete</tb-icon>
              </button>
            </ng-template>
            <div fxHide fxShow.gt-md fxFlex fxLayout="row" fxLayoutAlign="end">
              <ng-container [ngTemplateOutlet]="rowActions"></ng-container>
            </div>
            <div fxHide fxShow.lt-lg fxFlex fxLayout="row" fxLayoutAlign="end">
              <button mat-icon-button
                      (click)="$event.stopPropagation()"
                      [matMenuTriggerFor]="cellActionsMenu">
                <mat-icon class="material-icons">more_vert</mat-icon>
              </button>
              <mat-menu #cellActionsMenu="matMenu" xPosition="before">
                <ng-container [ngTemplateOutlet]="rowActions"></ng-container>
              </mat-menu>
            </div>
          </mat-cell>
        </ng-container>
        <mat-header-row [ngClass]="{'mat-row-select': true}" *matHeaderRowDef="['deviceName', 'info', 'unitId', 'type', 'actions']; sticky: true"></mat-header-row>
        <mat-row *matRowDef="let slave; columns: ['deviceName', 'info', 'unitId', 'type', 'actions']"></mat-row>
      </table>
      <section [fxShow]="!textSearchMode && (dataSource.isEmpty() | async)" fxLayoutAlign="center center"
               class="mat-headline-5 tb-absolute-fill tb-add-new">
        <button mat-button class="connector"
                (click)="manageSlave($event)">
          <mat-icon class="tb-mat-96">add</mat-icon>
          <span>{{ 'gateway.add-slave' | translate }}</span>
        </button>
      </section>
    </div>
    <span [fxShow]="textSearchMode && (dataSource.isEmpty() | async)"
          fxLayoutAlign="center center"
          class="no-data-found" translate>
      widget.no-data-found
    </span>
  </div>
</div>
